package com.forte.demo.robot.db.ban;

import com.forte.demo.robot.db.tools.DbUtil;
import com.forte.demo.robot.expection.NotBanListInputException;
import com.forte.demo.robot.timetask.Listener;
import com.forte.qqrobot.beans.messages.msgget.GroupMsg;
import com.forte.qqrobot.sender.MsgSender;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * @author SitaNya
 * @date 2019/9/25
 * @email sitanya@qq.com
 * @qqGroup 162279609
 * 有任何问题欢迎咨询
 * <p>
 * 类说明:
 */
public class SelectBanList {

    private static Logger Log = LogManager.getLogger(SelectBanList.class.getName());
    /**
     * 查询其它录入人
     */
    public static String selectOthorInputBanQq(String qq, MsgSender msgSender, GroupMsg msgGroup) throws NotBanListInputException {
        String inputBot = "";
        try (Connection conn = DbUtil.getConnection()) {
            String sql = "select botId from qqBanListBak where qqId=? ";
            try (PreparedStatement ps = conn.prepareStatement(sql)) {
                ps.setString(1, qq);
                try (ResultSet set = ps.executeQuery()) {
                    while (set.next()) {
                        inputBot = set.getString("botId");
                    }
                }
            }
        } catch (SQLException e) {
            throw new NotBanListInputException(msgSender,msgGroup);
        }
        return inputBot;
    }

    /**
     * 查询其它录入人
     */
    public static String selectOthorInputBanGroup(String groupId, MsgSender msgSender, GroupMsg msgGroup) throws NotBanListInputException {
        String inputBot = null;
        try (Connection conn = DbUtil.getConnection()) {
            String sql = "select botId from groupBanListBak where groupId=?";
            try (PreparedStatement ps = conn.prepareStatement(sql)) {
                ps.setString(1, groupId);
                try (ResultSet set = ps.executeQuery()) {
                    while (set.next()) {
                        inputBot = set.getString("botId");
                    }
                }
            }
        } catch (SQLException e) {
            throw new NotBanListInputException(msgSender,msgGroup);
        }
        return inputBot;
    }

    public String selectQqBanInfo(long qqId){
        StringBuilder stringBuilder=new StringBuilder();
        try (Connection conn = DbUtil.getConnection()) {
            String sql = "select botId, qqId, reason, date_format(time,'%Y-%m-%d %H:%i:%s') as time from qqBanListBak where qqId=?";
            try (PreparedStatement ps = conn.prepareStatement(sql)) {
                ps.setLong(1,qqId);
                try (ResultSet set = ps.executeQuery()) {
                    while (set.next()) {
                        stringBuilder.append("状态: 已被拉黑 录入时间:").append(set.getString("time")).append(" 负责人: ").append(set.getString("botId")).append(" 原因: ").append(set.getString("reason"));
                    }
                }
            }
        } catch (SQLException e) {
            Log.error(e.getMessage(), e);
        }
        return stringBuilder.toString();
    }

    public String selectGroupBanInfo(long groupId){
        StringBuilder stringBuilder=new StringBuilder();
        try (Connection conn = DbUtil.getConnection()) {
            String sql = "select botId, groupId, reason, date_format(time,'%Y-%m-%d %H:%i:%s') as time from groupBanListBak where groupId=?";
            try (PreparedStatement ps = conn.prepareStatement(sql)) {
                ps.setLong(1,groupId);
                try (ResultSet set = ps.executeQuery()) {
                    while (set.next()) {
                        stringBuilder.append("状态: 已被拉黑 录入时间:").append(set.getString("time")).append(" 负责人: ").append(set.getString("botId")).append(" 原因: ").append(set.getString("reason"));
                    }
                }
            }
        } catch (SQLException e) {
            Log.error(e.getMessage(), e);
        }
        return stringBuilder.toString();
    }

    public ArrayList<String> selectBanHistory(long Id){
        ArrayList<String> banHistorys=new ArrayList<>();
        try (Connection conn = DbUtil.getConnection()) {
            String sql = "select botId, Id, reason, isGroup, date_format(inputTime,'%Y-%m-%d %H:%i:%s') as inputTime, date_format(removeTime,'%Y-%m-%d %H:%i:%s') as removeTime from banHistoryBak where Id=?";
            try (PreparedStatement ps = conn.prepareStatement(sql)) {
                ps.setLong(1,Id);
                try (ResultSet set = ps.executeQuery()) {
                    while (set.next()) {
                        StringBuilder stringBuilder=new StringBuilder();
                        if (set.getBoolean("isGroup")){
                            stringBuilder.append("群:\t");
                        }else{
                            stringBuilder.append("个人:\t");
                        }
                        stringBuilder.append("状态: 曾拉黑被解除 录入时间:").append(set.getString("inputTime")).append(" 解除时间: ").append(set.getString("removeTime")).append(" 负责人: ").append(set.getString("botId")).append(" 原因: ").append(set.getString("reason"));
                        banHistorys.add(stringBuilder.toString());
                    }
                }
            }
        } catch (SQLException e) {
            Log.error(e.getMessage(), e);
        }
        return banHistorys;
    }
}
